﻿using System;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace Microsoft.ServiceModel.Samples
{
    // Define a service contract.
    [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
    public interface ICalculator
    {
        [OperationContract]
        void Add(double n1, double n2);
        [OperationContract]
        void Subtract(double n1, double n2);
        [OperationContract]
        void Multiply(double n1, double n2);
        [OperationContract]
        void Divide(double n1, double n2);

    }


    #region oth2

    // Service class that implements the service contract.
    // Added code to write output to the console window.
    public class CalculatorService : ICalculator
    {
        public void Add(double n1, double n2)
        {
            double result = n1 + n2;
            Console.WriteLine("Add({0},{1}) = {2} ", n1, n2, result);
        }

        public void Subtract(double n1, double n2)
        {
            double result = n1 - n2;
            Console.WriteLine("Subtract({0},{1}) = {2}", n1, n2, result);
        }

        public void Multiply(double n1, double n2)
        {
            double result = n1 * n2;
            Console.WriteLine("Multiply({0},{1}) = {2}", n1, n2, result);
        }

        public void Divide(double n1, double n2)
        {
            double result = n1 / n2;
            Console.WriteLine("Divide({0},{1}) = {2}", n1, n2, result);
        }

        public string SayHello(string name)
        {
            Console.WriteLine("SayHello({0})", name);
            return "Hello " + name;
        }

    }

    #endregion

    class Program
    {
        static void Main(string[] args)
        {

            #region oth1

            // Step 1 of the address configuration procedure: Create a URI to serve as the base address.
            Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service");

            // Step 2 of the hosting procedure: Create ServiceHost
            ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);

            try
            {


                // Step 3 of the hosting procedure: Add a service endpoint.
                selfHost.AddServiceEndpoint(
                    typeof(ICalculator),
                    new WSHttpBinding(),
                    "CalculatorService");


                // Step 4 of the hosting procedure: Enable metadata exchange.
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                selfHost.Description.Behaviors.Add(smb);

                // Step 5 of the hosting procedure: Start (and then stop) the service.
                selfHost.Open();
                Console.WriteLine("The service is ready.");
                Console.WriteLine("Press <ENTER> to terminate service.");
                Console.WriteLine();
                Console.ReadLine();

                // Close the ServiceHostBase to shutdown the service.
                selfHost.Close();
            }
            catch (CommunicationException ce)
            {
                Console.WriteLine("An exception occurred: {0}", ce.Message);
                selfHost.Abort();
            }

            #endregion

        }
    }
}

